//
//  SelectTimeTableViewCell.m
//  Rail
//
//  Created by hychen on 2018/8/2.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "SelectTimeTableViewCell.h"
#import "HcdDateTimePickerView.h"

@interface SelectTimeTableViewCell ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UILabel *startLabel;
@property (nonatomic, strong) UILabel *endLabel;

@property (nonatomic, strong) UITextField *startField;
@property (nonatomic, strong) UITextField *endField;
@property (nonatomic, weak) UITextField *currentField;

@property (nonatomic, strong) HcdDateTimePickerView *startPickerView;
@property (nonatomic, strong) HcdDateTimePickerView *endPickerView;
@end

@implementation SelectTimeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    
    BMWeakSelf
    self.tipLabel = [UILabel labelWithText:@"请选择时间段" textColor:Color_RGB(127, 127, 127) font:[UIFont systemFontOfSize:16]];
    self.tipLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.tipLabel];
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(weakSelf.contentView);
        make.height.mas_equalTo(40);
        make.width.equalTo(weakSelf.contentView);
    }];
    
    self.startLabel = [UILabel labelWithText:@"起始时间:" textColor:Color_RGB(124, 223, 240) font:[UIFont systemFontOfSize:17]];
    self.startLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.startLabel];
    [self.startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(weakSelf.tipLabel.mas_bottom);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    self.endLabel = [UILabel labelWithText:@"结束时间:" textColor:Color_RGB(255, 50, 64) font:[UIFont systemFontOfSize:17]];
    self.endLabel.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.endLabel];
    [self.endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView);
        make.top.equalTo(weakSelf.startLabel.mas_bottom).offset(5);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    self.startField = [UITextField new];
    self.startField.font = [UIFont systemFontOfSize:18.f];
    self.startField.textColor = Color_RGB(124, 223, 240);
    self.startField.textAlignment = NSTextAlignmentCenter;
    self.startField.text = [_fromDate formatYearMonthDayHourSecond_lib];
    self.startField.delegate = self;
    [self.contentView addSubview:self.startField];
    [self.startField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView);
        make.left.equalTo(self.startLabel.mas_right).offset(35);
        make.top.equalTo(self.startLabel);
        make.bottom.equalTo(self.startLabel);
    }];
    
    self.endField = [UITextField new];
    self.endField.font = [UIFont systemFontOfSize:18.f];
    self.endField.textColor = Color_RGB(255, 50, 64);
    self.endField.textAlignment = NSTextAlignmentCenter;
    self.endField.text = [_toDate formatYearMonthDayHourSecond_lib];
    self.endField.delegate = self;
    [self.contentView addSubview:self.endField];
    [self.endField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView);
        make.left.equalTo(self.endLabel.mas_right).offset(35);
        make.top.equalTo(self.endLabel);
        make.bottom.equalTo(self.endLabel);
    }];
    
}
#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    
    self.currentField = textField;
    
    if (textField == self.startField) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.startPickerView];
        self.startPickerView.maxDate = [NSDate date];
        [self.startPickerView showHcdDateTimePicker];
    }
    else if (textField == self.endField) {
        [[UIApplication sharedApplication].keyWindow addSubview:self.endPickerView];
        self.endPickerView.maxDate = [NSDate date];
        [self.endPickerView showHcdDateTimePicker];
    }
    
    return NO;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (HcdDateTimePickerView *)startPickerView {
    if (!_startPickerView) {
        _startPickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateTimeMode defaultDateTime:_fromDate];
        _startPickerView.topViewColor = Color_RGB(247, 247, 247);
        _startPickerView.buttonTitleColor = Color_RGB(75, 200, 175);
        BMWeakSelf
        [_startPickerView setClickedOkBtn:^(NSString *dateTimeStr) {
            weakSelf.startField.text = dateTimeStr;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            //赋值日期
            weakSelf.fromDate = [fmt dateFromString:dateTimeStr];
            if (weakSelf.BlockSelectFromDate) {
                weakSelf.BlockSelectFromDate(weakSelf.fromDate);
            }
        }];
        NSInteger currYear = _fromDate.year;
        [_startPickerView setMinYear:currYear-1];
        [_startPickerView setMaxYear:currYear];
    }
    return _startPickerView;
}

- (HcdDateTimePickerView *)endPickerView {
    if (!_endPickerView) {
        _endPickerView = [[HcdDateTimePickerView alloc] initWithDatePickerMode:DatePickerDateTimeMode defaultDateTime:_toDate];
        _endPickerView.topViewColor = Color_RGB(247, 247, 247);
        _endPickerView.buttonTitleColor = Color_RGB(75, 200, 175);
        BMWeakSelf
        [_endPickerView setClickedOkBtn:^(NSString *dateTimeStr) {
            weakSelf.endField.text = dateTimeStr;
            NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
            fmt.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            //赋值日期
            weakSelf.toDate = [fmt dateFromString:dateTimeStr];
            if (weakSelf.BlockSelectToDate) {
                weakSelf.BlockSelectToDate(weakSelf.toDate);
            }
            if (weakSelf.toDate == [weakSelf.toDate earlierDate:weakSelf.fromDate]) {
                weakSelf.fromDate = [weakSelf.toDate dateBySubtractingMinutes:5];
                if (weakSelf.BlockSelectFromDate) {
                    weakSelf.BlockSelectFromDate(weakSelf.fromDate);
                }
                weakSelf.startField.text = [weakSelf.fromDate formatYearMonthDayHourSecond_lib];
            }
        }];
        NSInteger currYear = _toDate.year;
        [_endPickerView setMinYear:currYear-1];
        [_endPickerView setMaxYear:currYear];
    }
    return _endPickerView;
}

- (void)setFromDate:(NSDate *)fromDate {
    _fromDate = fromDate;
    self.startField.text = [self.fromDate formatYearMonthDayHourSecond_lib];
}

- (void)setToDate:(NSDate *)toDate {
    _toDate = toDate;
    self.endField.text = [self.toDate formatYearMonthDayHourSecond_lib];
}
@end
